<?php
require_once 'PHPUnit/Framework.php';
require_once 'config.php';
//require_once 'lib/controllers/nddList.php';


class NddControllerTest extends PHPUnit_Framework_TestCase
{
	public function testDoList()
	{
		$controller = new NddListController('list', new ezcMvcRequest());
		$list = $controller->doList();
		
		$this->assertTrue(isset($list->variables['ndds']));		//check if there are results
		
		$ndds = $list->variables['ndds'];
		$this->assertGreaterThan(0,count($ndds));				//how many results ?
		
		$ndd = array_pop($ndds);
		$this->assertTrue($ndd instanceof NomDomaine );			//check the class of the first element
	}
	
	public function testDoSearch()
	{
		$controller = new NddListController('list', new ezcMvcRequest());
		$list = $controller->doSearch();
		
		//with no criteria, we should get the whole list, just like doList()
		$this->assertTrue(isset($list->variables['ndds']));		//check if there are results
		$ndds = $list->variables['ndds'];
		$this->assertGreaterThan(0,count($ndds));				//how many results ?
		$ndd = array_pop($ndds);
		$this->assertTrue($ndd instanceof NomDomaine );			//check the class of the first element
		
		//test the search criteria
		$_POST['recherche'] = 'bgjhbsdbgdbsjg';	//dummy search
		$list = $controller->doSearch();
		$ndds = $list->variables['ndds'];
		$this->assertEquals(0,count($ndds));	
		
		//test search with special chars
		$_POST['recherche'] = '%^*\k/;+po\uy';
		$list = $controller->doSearch();
		$ndds = $list->variables['ndds'];
		$this->assertEquals(0,count($ndds));	
		
		//test a simple criteria that should have result
		$_POST['recherche'] = '.com';
		$list = $controller->doSearch();
		$ndds = $list->variables['ndds'];
		$this->assertGreaterThan(0,count($ndds));	
		$ndd = array_pop($ndds);
		$this->assertTrue($ndd instanceof NomDomaine );	
		
		//test a date
		$_POST['recherche'] = '12/01/2101';
		$list = $controller->doSearch();
		$ndds = $list->variables['ndds'];
		$this->assertGreaterThan(0,count($ndds));	
		
		$_POST['recherche'] = '12/01/30';
		$list = $controller->doSearch();
		$ndds = $list->variables['ndds'];
		$this->assertGreaterThan(0,count($ndds));	
	}
	
	/**
	 * Create a NDD, update it, and delete it
	 */
	public function testDoCreateNdd()
	{
		//get db instance
		$psession = NddSession::getSession();
		
		$controller = new NddManageController('save', new ezcMvcRequest());
		
		//fake data
		$rand = rand(1,100000);
		$_POST = array ( 'nddId' => '',
						 'agresso' => "L$rand",
						 'societe' => "Sté SQLI",
						 'client' => "Jacques Durant",
						 'domaine' => 'www.sqli.fr',
						 'expiration' => '29/04/2010',
						 'email' => "jdurant@sqli.com",
						 'tel' => "06 12 45 78 98",
						 'hebergement' => "durant hébergement",
						 'date_hebergement' => "01/01/1998",
						 'facturation' => "texte de la facturation \ntexte de la facturation \n\ntexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturationtexte de la facturation",
						 'commentaire' => "mon commentaire, l'arbre de la \forêt" );
		
		//
		//Create
		//
		$creation = $controller->doCreateNdd();
		$this->assertEquals(0,count($list->variables['error']));		//check if there are error
		
		//search the ndd created (only one)
		$controllerList = new NddListController('list', new ezcMvcRequest());
		$_POST['recherche'] = "L$rand";
		
		$list = $controllerList->doSearch();
		$ndds = $list->variables['ndds'];
		$this->assertEquals(1,count($ndds));

		$ndd = array_pop($ndds);
		$this->assertTrue($ndd instanceof NomDomaine );	
		
		//check error messages
		$_POST['expiration'] = '01/122/2010';
		$_POST['date_hebergement'] = '01-01-2010';
		$creation = $controller->doCreateNdd();
		$this->assertEquals(2,count($creation->variables['errors']));		//check if there are error
		
		//
		//Update
		//
		$_POST['nddId'] = $ndd->id;
		$_POST['agresso'] .= "-".rand(0,9);
		$_POST['expiration'] = '01/02/2010';
		$_POST['date_hebergement'] = '01/01/2010';
		
		$update = $controller->doUpdateNdd();
		$this->assertEquals(0,count($update->variables['errors']));		//check if there are error
		
		$ndd = $psession->load( 'NomDomaine', $_POST['nddId'] );
		$this->assertTrue($ndd instanceof NomDomaine );	
		$this->assertEquals($_POST['agresso'],$ndd->agresso);
		
		$_POST['expiration'] = '01/122/2010';
		$_POST['date_hebergement'] = '01-01-2010';
		$update = $controller->doUpdateNdd();
		$this->assertEquals(2,count($update->variables['errors']));	
		
		//
		//Delete
		//
		$delete = $controller->doDelete();
		$this->assertEquals(0,count($delete->variables['errors']));
		
		$_POST['nddId'] = null;
		$delete = $controller->doDelete();
		$this->assertEquals(1,count($delete->variables['errors']));
		
		$_POST['nddId'] = "azerty";
		$delete = $controller->doDelete();
		$this->assertEquals(1,count($delete->variables['errors']));
		
	}
	
}
?>